package com.johann.datatype;

import java.math.BigDecimal;
import java.util.Scanner;

import static java.lang.Math.sqrt;

/**
 * @Description: java的两大浮点类型
 * 根据IEEE 754标准
 *                                                                   符号位为1 ∪ 符号位为0
 * float        4       [-3.40282346638528860e+38 , -1.40129846432481707e-45] ∪ [1.40129846432481707e-45 ~ 3.40282346638528860e+38]
 * double       8       [-1.79769313486231570e+308,-4.94065645841246544e-324] ∪  [4.94065645841246544e-324,1.79769313486231570e+308]
 *
 * 以float为例，占32位，分 符号位1、指数位8、尾数位23
 * 如果在数值计算中不允许有任何舌入误差，建议用BigDecimal类
 */
public class JavaFloatingPointType {
    private final static float ftNumber = Float.MAX_VALUE;
    private final static float ftNumber2 = Float.MIN_VALUE;

    private final static double dbNum=Double.POSITIVE_INFINITY;     //正整数除以0
    private final static double dbNum1=Double.NEGATIVE_INFINITY;    //负整数除以0
    private final static double dbNum2=Double.NaN;                  //零除以零，或者负数的平方根


    public static void main(String[] args) {
        System.out.println(ftNumber);       //3.4028235E38
        System.out.println(ftNumber2);      //1.4E-45
        float f1=1.23f;     //f最好带上，和double做区分
        double d1=3.45d;    //d可带可不带,默认不带

       //检测某一个数是不是NaN

        Scanner scanner = new Scanner(System.in);
        double judge  =  scanner.nextDouble();
        if (judge==Double.NaN) {        //不要这么做
        }
        if (Double.isNaN(sqrt(judge))) {      //应该这么做
            System.out.println("Not a Number "+sqrt(judge));   //Not a NumberNaN
        }
        double judge1=judge-1/judge;
        if (Double.isInfinite(judge1)) {
            System.out.println("正无穷还是负无穷？"+judge1);     //-Infinity或者Infinity
        }
    }

}
